import { ComponentClass } from 'react'
import { connect } from '@tarojs/redux'

import Taro, { Component, Config } from '@tarojs/taro'

import { AtLoadMore } from 'taro-ui'

import HomeShare from '../../../containers/HomeShare'
import HomeContent from '../../../containers/HomeContent'
import HomeHandle from '../../../containers/HomeHandle'
import XPage from '../../../components/XPage'

import { queryListMore } from '../../../actions/goods'

import "../testImg.scss"

type PageStateProps = {
  goods: object | {},
}

type PageDispatchProps = {
  fetchList: () => void
}

type PageOwnProps = {}

type PageState = {}

type IProps = PageStateProps & PageDispatchProps & PageOwnProps

interface Lists {
  props: IProps
}

@connect(({ goods }) => ({
  goods
}), (dispatch) => ({
  fetchList(params) {
    dispatch(queryListMore(params))
  }
}))
class Lists extends Component {
  config: Config = {
    navigationBarTitleText: '幸运草小程序'
  }

  componentWillMount() {
    // 初始化数据
    this.props.fetchList({type: 1, sorter: 'id_ascend'})

    // console.log('哈哈哈')

    // Taro.checkSession().then(results => {
    //   console.log(results, 111)
    // }).catch(()=>{

    //   Taro.setStorage({key: 'getUserInfo',data: null})

    // })

    // 获取用户信息
    // Taro.getUserInfo().then(res=>{
    //   console.log(res)
    // })
  }

  //分享按钮函数
  onShareAppMessage() {
      return {
          title: '幸运草小程序',
          path: 'pages/home/index?userId=0',
          success(res) {
              console.log("转发成功:" + JSON.stringify(res))
          },
          fail(res) {
              console.log("转发失败:" + JSON.stringify(res))
          }
      }
  }

  onToPage = msg => {
    msg.stopPropagation()

    Taro.navigateTo({
      url: '/pages/profile/index'
    })
  }

  onLoadMore = msg => {
    msg.stopPropagation()

    Taro.navigateTo({
      url: '/pages/profile/index'
    })
  }

  render() {
    return (
      <XPage>
        {
          this.props.goods.isLoading
          ?
          <AtLoadMore loadingText='加载中...' status={this.props.goods.isLoading ? 'loading' : 'noMore'} />
          :
          this.props.goods.data.data.map((row) => <HomeContent  queryRow={row} key={row.id} onClick={this.onToPage} />)
        }
      </XPage>
    )
  }
}

export default Lists as ComponentClass<PageOwnProps, PageState>